/*
 * comlan
 */

package com.promfy.timetable.ui;


import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.promfy.timetable.R;
import com.promfy.timetable.db.OneClassDBService;
import com.promfy.timetable.domain.OneClass;

public class CourseConfigView extends LinearLayout implements OnClickListener{

	
	private Handler handler = new Handler(){

		@Override
		public void handleMessage(Message msg) {
			int what = msg.what;
			switch (what) {
			
			default:
				break;
			}
			super.handleMessage(msg);
		}
		
	};
	
	private LayoutInflater inflater;

	private Context context;

	private View view;

	private ListView listView;

	private List<OneClass> oneClassList = new ArrayList<OneClass>();

	private BaseAdapter adapter;
		
	public CourseConfigView(Context context) {
		super(context);
		this.context = context;
	
		initView();
		initData();
	}

	
	
	public CourseConfigView(Context context, AttributeSet attrs) {
		super(context, attrs);

	}



	private void initView(){
		inflater = LayoutInflater.from(context);
		view = inflater.inflate(R.layout.course_config, this);
		listView = (ListView)view.findViewById(R.id.listview_course_config);
		adapter = new CourseConfigAdapter(context, 0, R.layout.course_config_listview_item, oneClassList);
		listView.setAdapter(adapter);
	}
	
	private void initData() {
//		OneClass oc1 = new OneClass(1, 1, "aaa", "aaaaa", "aaaa", OneClass.CLASS_PROP_TAKE_EVERY_WEEK);
//		OneClass oc2 = new OneClass(1, 3, "bbb", "bbbbb", "bbbb", OneClass.CLASS_PROP_TAKE_EVERY_WEEK);
//		OneClass oc3 = new OneClass(1, 5, "ccc", "ccccc", "cccc", OneClass.CLASS_PROP_TAKE_EVERY_WEEK);
//		oneClassList.add(oc1);
//		oneClassList.add(oc2);
//		oneClassList.add(oc3);
		oneClassList.clear();
		for(int i = 0; i<7; i++){
			for(int j =0; j < 5; j++){
				OneClass oc = new OneClass(i+1, (j+1)*2-1, "aaa", "bbb", "ccc", OneClass.CLASS_PROP_TAKE_EVEN_WEEK);
				oneClassList.add(oc); 
			} 
		}
		adapter.notifyDataSetChanged();
//		//test
//		try { 
//			insertOrUpdate(context, oneClassList);
//		} catch (Exception e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		} 
	}
	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		//restart
		case 1:
		
			break;
		//back to mode
		case 2:

			break;
		//exit
		case 3:
			
			break;
		default:
			break;
		}
	}

	private void insertOrUpdate(Context context, List<OneClass> innerOneClassList) throws Exception{
		OneClassDBService ocdb = new OneClassDBService(context);
		List<OneClass> dbOneClassList = ocdb.getList();
		if(dbOneClassList == null || dbOneClassList.size() == 0){
			for(OneClass oc : innerOneClassList){
				ocdb.insert(oc);
			}
			return;
		}
		for(OneClass oc : innerOneClassList){
			for(OneClass dboc : dbOneClassList){
				if(oc.getWeek() == dboc.getWeek() && oc.getCourse() == dboc.getCourse()){
					ocdb.update(oc);
				}else{
					ocdb.insert(oc);
				}
			}
		}
	}
	
	private class CourseConfigAdapter extends ArrayAdapter {

		private LayoutInflater inflater;
		private List<OneClass> classes;

		private Bitmap collapseIcon;
		private Bitmap expandIcon;

		public CourseConfigAdapter(Context context, int resource, int textViewResourceId, List objects) {
			super(context, resource, textViewResourceId, objects);
			inflater = LayoutInflater.from(context);
			classes = (ArrayList<OneClass>) objects; 

		}

		@Override
		public Object getItem(int position) {
			return classes.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder holder;
			if (convertView == null) {
				convertView = inflater.inflate(R.layout.course_config_listview_item, null);

				holder = new ViewHolder();
				holder.courseName = (EditText) convertView.findViewById(R.id.course_name_course_config_listview_item);
				holder.location = (EditText) convertView.findViewById(R.id.location_course_config_listview_item);
				holder.teacher = (EditText) convertView.findViewById(R.id.teacher_course_config_listview_item);
				holder.week = (TextView) convertView.findViewById(R.id.week_config_listview_item);
				holder.course = (TextView) convertView.findViewById(R.id.course_config_listview_item);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}
			
			try {
				OneClass oc = (OneClass) getItem(position);
				holder.courseName.setText(oc.getClassName());
				holder.location.setText(oc.getLocation());
				holder.teacher.setText(oc.getTeacher());
				holder.week.setText(mapToWeek(oc.getWeek()));
				holder.course.setText(oc.getCourse()+"/"+(oc.getCourse()+1));
				
			} catch (Exception e) {
				e.printStackTrace();
			}

			return convertView;
		}

		class ViewHolder {
			EditText courseName;
			EditText location;
			EditText teacher;
			
			TextView week;
			TextView course;
		}

		@Override
		public int getCount() {
			return classes.size();
		}

	}

	private String mapToWeek(int week){
		switch(week){
		case 1:
			return  "周一";
		case 2:
			return  "周二";
		case 3:
			return  "周三";
		case 4:
			return  "周四";
		case 5:
			return  "周五";
		case 6:
			return  "周六";
		case 7:
			return  "周日";
		default:
			return "";
		}
	}


}
